/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  SignRecordActivity.java   
 * @Package com.jiyi.activity.datainput   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2018-1-9 下午2:35:40   
 * @version V1.0     
 */  
package com.jiyi.activity.datainput.sm;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.DataInputSignRecordAdapter;
import com.jiyi.bean.datainput.DataInputSignRecordBean;
import com.jiyi.bean.datainput.DataInputSignRecordParent;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**   
 * @ClassName:  SignRecordActivity   
 * @Description:TODO(签到记录)   
 * @author: HYM  
 * @date:   2018-1-9 下午2:35:40   
 *      
 */
public class SignRecordActivity extends ActivityBase implements OnRefreshListener{

	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.refreshLayout)
	SwipeRefreshLayout refreshLayout;
	@BindView(R.id.srlTip)
	SwipeRefreshLayout srlTip;

	@BindView(R.id.mainList)
	ExpandableListView mainList;

	DataInputSignRecordAdapter dataInputSignRecordAdapter;

	ArrayList<DataInputSignRecordParent> dataInputSignRecordParents = new ArrayList<DataInputSignRecordParent>();

	String userId;
	
	@Override
	protected void onCreate(@Nullable Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_sign_record);
		ButterKnife.bind(this);
		init();
	}
	
	void init() {
		getIntentData();
		setTop();
		setAdapter();
		getSignData();
	}
	
	/**
	 * @author: HYM
	 * @Title: getIntentData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-7-25
	 */
	private void getIntentData() {
		userId = getIntent().getStringExtra("userId");
	}

	/**
	 * @author: HYM
	 * @Title: setTop
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-15
	 */
	private void setTop() {
		tvBack.setText("");
		tvTitle.setText("签到记录");
		tvSearch.setVisibility(View.INVISIBLE);
	}

	private void setAdapter() {
		// 设置refreshLayout的监听
		refreshLayout.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE,
				Color.GREEN);
		refreshLayout.setOnRefreshListener(this);
		srlTip.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE,
				Color.GREEN);
		srlTip.setOnRefreshListener(this);

		dataInputSignRecordAdapter = new DataInputSignRecordAdapter(this, dataInputSignRecordParents);
		mainList.setAdapter(dataInputSignRecordAdapter);
		mainList.setGroupIndicator(null);// 这个是去掉父级的箭头
		// 点击不能收缩
		mainList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return true;
			}
		});
		
	}

	private void getSignData() {

		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		if(!TextUtils.isEmpty(userId))
		{
			params.put("user_id", userId);
		}

		VolleyRequest.StringRequestPost(this, WebApi.DATA_INPUT_SIGN_RECORD, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						if (isFinishing()) {
							return;
						}
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								Boolean hasData = false;
								try {
									JSONArray dataJson = new JSONArray(data);
									hasData = false;
								} catch (Exception e) {
									hasData = true;
								}

								if (hasData) {
									JSONObject dataJson = new JSONObject(data);
									Iterator<String> it = dataJson.keys();
									srlTip.setVisibility(View.GONE);
									refreshLayout.setVisibility(View.VISIBLE);
									dataInputSignRecordParents.clear();
									while (it.hasNext()) {
										String json_key = it.next();
										Object json_value = dataJson
												.get(json_key);
										if (json_value != null) {
											DataInputSignRecordParent disrp = new DataInputSignRecordParent();
											disrp.setTitle(json_key+"年");
											List<DataInputSignRecordBean> temp = new Gson().fromJson(
													json_value.toString(),
													new TypeToken<List<DataInputSignRecordBean>>() {
													}.getType());
											disrp.setChildList(temp);
											dataInputSignRecordParents.add(disrp);
										}
									}
									
									dataInputSignRecordAdapter.notifyDataSetChanged();
									for (int i = 0; i < dataInputSignRecordParents.size(); i++) {
										mainList.expandGroup(i);
									}

								} else {
									srlTip.setVisibility(View.VISIBLE);
									refreshLayout.setVisibility(View.GONE);
								}

							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							refreshLayout.setRefreshing(false);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						refreshLayout.setRefreshing(false);
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}

	@Override
	public void onRefresh() {
		getSignData();
	}

	@OnClick({ R.id.tvBack })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		}
	}


}
